package com.wyvoid.forum.web.action.forum;

import com.wyvoid.forum.web.action.BaseAction;
import com.wyvoid.forum.web.action.IndexAction;
import com.wyvoid.forum.web.tag.ForumFunctions;
import com.wyvoid.forum.web.search.SearchBean;
import com.wyvoid.forum.service.IForumService;
import com.wyvoid.forum.service.ITopicService;
import com.wyvoid.forum.domain.Forum;
import com.wyvoid.forum.domain.Topic;
import com.wyvoid.forum.domain.SessionUser;
import com.wyvoid.core.persistent.hibernate.HibernatePage;
import com.wyvoid.core.persistent.Pagination;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import org.apache.commons.lang.StringUtils;

/**
 * <p>51void.com</p>
 * User:cjp
 * Date: 2007-5-26
 * Time: 17:32:29
 */
@UrlBinding(value = "/forum.do")
public class ForumAction extends BaseAction {
    private Forum parentForum;
    private List<Forum> subForums;
    private HibernatePage<Topic> topics;
    private int curPage;

    private List<Forum> forums;

    private long forumId;
    @SpringBean
    private IForumService forumService;
    @SpringBean
    private ITopicService topicService;
    private SearchBean searchBean;

    private int styleId;


    public Forum getParentForum() {
        return parentForum;
    }

    public List<Forum> getSubForums() {
        return subForums;
    }

    public HibernatePage<Topic> getTopics() {
        return topics;
    }

    public List<Forum> getForums() {
        return forums;
    }

    public void setForumService(IForumService forumService) {
        this.forumService = forumService;
    }

    public void setTopicService(ITopicService topicService) {
        this.topicService = topicService;
    }


    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public void setForumId(long forumId) {
        this.forumId = forumId;
    }

    public int getStyleId() {
        return styleId;
    }

    public void setStyleId(int styleId) {
        this.styleId = styleId;
    }


    public SearchBean getSearchBean() {
        return searchBean;
    }

    public void setSearchBean(SearchBean searchBean) {
        this.searchBean = searchBean;
    }

    @DefaultHandler
    public Resolution viewForum() {
        forums = forumService.getOpenedForums();
        // 论坛数据
        if (forumId == 0) {
            return new RedirectResolution(IndexAction.class);
        }
        parentForum = forumService.getForum(forumId);
        if (curPage == 0) {
            curPage = 1;
        }

        Pagination pagination = new Pagination();
        pagination.setPageNumber(curPage);
//        pagination.setPageSize(1);
        subForums = forumService.getForums(forumId);
        topics = topicService.getTopics(forumId, null, pagination);
        return getBBSResolution("viewForum");
    }

    public Resolution changeStyle() {
        HttpServletResponse response = context.getResponse();
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        context.getRequest().getSession().setAttribute("styleId", styleId);

        String targetUrl = context.getRequest().getParameter("referer");
        if (targetUrl == null) {
            targetUrl = context.getRequest().getHeader("referer");
        }
        if (StringUtils.isNotBlank(targetUrl) && targetUrl.indexOf("leftMenu.do") == -1) {
            return new RedirectResolution(targetUrl, false).addParameter("ra", Math.random());
        } else {
            return new RedirectResolution("/").addParameter("ra", Math.random());
        }
    }

    public Resolution search() {
        if (curPage == 0) {
            curPage = 1;
        }

        Pagination pagination = new Pagination();
        pagination.setPageNumber(curPage);
//        pagination.setPageSize(1);
        subForums = forumService.getForums(forumId);
        topics = topicService.searchTopics2(searchBean, pagination);
        return getBBSResolution("viewForum");
    }

    public Resolution digest() {
        searchBean = new SearchBean();
        searchBean.setSearchFilter(1);
        return search();
    }
}
